#include "node.h"

Node::Node(void) { }

Node::~Node(void)
{
	std::vector<Node*>::iterator it;

	// Go through each child and delete it
	while (this->m_children.size() > 0)
	{
		// Get iterator to first child
		it = this->m_children.begin();
		// Delete it
		delete (*it);
		// Remove it from the list
		this->m_children.erase(it);
	}
}

void Node::AddChild(Node* newNode, unsigned int index)
{
	// Make sure index is in range
	if (index > this->m_children.size())
		index = this->m_children.size();

	// Create iterator for insertion
	std::vector<Node*>::iterator it = this->m_children.begin();
	for (unsigned int i = 0; i < index; i++)
		it++;

	// Insert new node
	this->m_children.insert(it, newNode);
}

void Node::RemoveChild(unsigned int child)
{
	// Make sure child is in range
	if (child > this->m_children.size())
		child = this->m_children.size();

	// Create iterator for removing
	std::vector<Node*>::iterator it = this->m_children.begin();
	for (unsigned int i = 0; i < child; i++)
		it++;

	// Remove node
	this->m_children.erase(it);
}

bool Node::SwapChildren(unsigned int child1, unsigned int child2)
{
	/*if (child1 != child2)
	{
		Node* node1 = NULL, *node2 = NULL;
		std::vector<Node*>::iterator it1, it2;

		it1 = it2 = this->m_children.begin();
		for (unsigned int i = 0; i < this->m_children.size(); i++)
		{
			if (i == child1)
				node1 = (*it1);
			else if (i == child2)
				node2 = (*it2);
		}
	}*/
	return false;
}

bool Node::HasChildren(void)
{
	if (this->m_children.size() == 0)
		return false;
	return true;
}

Node* Node::Parent() { return this->m_parent; }